package edu.ccut.saturn.studio.wfg.editor;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.jface.resource.ImageDescriptor;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.ShapesPlugin;
import edu.ccut.saturn.studio.wfg.model.Connection;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAutoActivityNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnEndNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnHumanNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnInformationNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnRouterNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnStartNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnSubActivityNode;

final class WfgEditorPaletteFactory {
	
	private static PaletteContainer createShapesDrawer() {
		PaletteDrawer componentsDrawer = new PaletteDrawer(Messages.getString("WfgEditorPaletteFactory.Drawer"));   //$NON-NLS-1$
		CombinedTemplateCreationEntry startEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.Start"), "startNode", SaturnStartNode.class,    //$NON-NLS-1$
				new SimpleFactory(SaturnStartNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SMALL_START_ICON), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_START_ICON));
		
		CombinedTemplateCreationEntry endEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.End"), "endNode", SaturnEndNode.class,    //$NON-NLS-1$
				new SimpleFactory(SaturnEndNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SMALL_END_ICON), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_END_ICON));
		
		CombinedTemplateCreationEntry humanEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.Man"), "humanNode", SaturnHumanNode.class,    //$NON-NLS-1$
				new SimpleFactory(SaturnHumanNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SMALL_HUMAN_ICON), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_HUMAN_ICON));
		
		CombinedTemplateCreationEntry routerEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.Router"), "routerNode", SaturnRouterNode.class,     //$NON-NLS-1$
				new SimpleFactory(SaturnRouterNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SMALL_ROUTER_ICON), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_ROUTER_ICON));
		
		CombinedTemplateCreationEntry autoActivityEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.AutoActivity"), "autoActivityNode", SaturnAutoActivityNode.class, //$NON-NLS-1$
				new SimpleFactory(SaturnAutoActivityNode.class), ImageDescriptor
				.createFromFile(ShapesPlugin.class, 
						Activator.SMALL_AUTO_ACTIVITY), ImageDescriptor
						.createFromFile(ShapesPlugin.class, 
								Activator.BIG_AUTO_ACTIVITY));
		
		CombinedTemplateCreationEntry subActivityEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.SubActivity"), "subActivityNode", SaturnSubActivityNode.class, //$NON-NLS-1$
				new SimpleFactory(SaturnSubActivityNode.class), ImageDescriptor
				.createFromFile(ShapesPlugin.class, 
						Activator.SMALL_SUB_ACTIVITY), ImageDescriptor
						.createFromFile(ShapesPlugin.class, 
								Activator.BIG_SUB_ACTIVITY));
		
		CombinedTemplateCreationEntry infoEntry = new CombinedTemplateCreationEntry(
				Messages.getString("WfgEditorPaletteFactory.Description"), "infoNode", SaturnInformationNode.class,    //$NON-NLS-1$
				new SimpleFactory(SaturnInformationNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SMALL_INFORMATION_ICON), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_INFORMATION_ICON));
		
		componentsDrawer.add(startEntry);
		componentsDrawer.add(humanEntry);
		componentsDrawer.add(endEntry);
		componentsDrawer.add(routerEntry);
		componentsDrawer.add(autoActivityEntry);
		componentsDrawer.add(subActivityEntry);
		componentsDrawer.add(infoEntry);
		return componentsDrawer;
	}

	static PaletteRoot createPalette() {
		PaletteRoot palette = new PaletteRoot();
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());
		return palette;
	}

	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		PaletteGroup toolGroup = new PaletteGroup(Messages.getString("WfgEditorPaletteFactory.Tool"));   //$NON-NLS-1$

		ToolEntry tool = new PanningSelectionToolEntry();
		toolGroup.add(tool);
		palette.setDefaultEntry(tool);

		toolGroup.add(new MarqueeToolEntry());

		toolGroup.add(new PaletteSeparator());

		tool = new ConnectionCreationToolEntry(Messages.getString("WfgEditorPaletteFactory.LinkLine"),   //$NON-NLS-1$
				"Create a solid-line connection", new CreationFactory() { 
					public Object getNewObject() {
						return null;
					}
					public Object getObjectType() {
						return Connection.SOLID_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(ShapesPlugin.class,
								Activator.SMALL_CONNECTION_ACTIVITY), ImageDescriptor 
						.createFromFile(ShapesPlugin.class,
								Activator.BIG_CONNECTION_ACTIVITY)); 
		toolGroup.add(tool);
		return toolGroup;
	}

	private WfgEditorPaletteFactory() {
	}
}